package com.javasm.mingming.upload.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;

import com.javasm.mingming.test.entity.TestStudent;
import com.javasm.mingming.test.service.TestStudentService;
import com.javasm.mingming.common.R;
import com.javasm.mingming.common.utils.QiniuKodoUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author javasm
 * @version 0.1
 * @className UploadController
 * @descriptioin:
 * @date 2025/1/18 11:24
 * @since jdk11
 */
@RestController
@RequestMapping("/upload")
public class UploadController {
    @Resource
    TestStudentService studentService;

    @PostMapping("/test1")
    public R upload1(MultipartFile file) throws IOException {
        //每次存100条
        EasyExcel.read(file.getInputStream(), TestStudent.class,new PageReadListener<TestStudent>(list->{
            studentService.saveBatch(list);
        })).sheet().doRead();
        return R.ok();
    }
    @PostMapping("/qiniu")
    public R upload2(MultipartFile file){
        String upload = QiniuKodoUtil.upload(file);
        return R.ok(upload);
    }

    @GetMapping("/token")
    public R token(){
        String token = QiniuKodoUtil.getToken();
        String domain = QiniuKodoUtil.getDomain();
        Map<String,String> map = new HashMap<>();
        map.put("token",token);
        map.put("domain",domain);
        return R.ok(map);
    }
}
